package com.dmpp.mes.service.impl;


import com.dmpp.mes.bean.RespEnum;
import com.dmpp.mes.bean.ResponseData;
import com.dmpp.mes.dao.CategoryMapper;
import com.dmpp.mes.dao.MaterialQualityMapper;
import com.dmpp.mes.parameterInfo.Category;
import com.dmpp.mes.parameterInfo.MaterialQuality;
import com.dmpp.mes.service.CategoryService;
import com.dmpp.mes.service.MaterialQualityService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(value = "MaterialQualityService")

public class MaterialQualityServiceImpl implements MaterialQualityService {

    @Autowired
    private MaterialQualityMapper mapper;

    @Override
    public ResponseData findAll(MaterialQuality materialQuality) {
        ResponseData responseData = new ResponseData();
        PageInfo pageInfo = new PageInfo<>(mapper.findAll(materialQuality));
        responseData.setData(pageInfo);
        return responseData;
    }

    @Override
    public ResponseData findByCode(MaterialQuality materialQuality) {
        ResponseData responseData = new ResponseData();
        responseData.setData(mapper.findByCode(materialQuality));
        return responseData;
    }

    @Override
    public ResponseData insert(MaterialQuality materialQuality) {
        ResponseData responseData = new ResponseData();
        MaterialQuality materialQuality1= mapper.findByCode(materialQuality);
        if (materialQuality1==null){
            mapper.insert(materialQuality);
        }else {
            responseData.setCodeEnum(RespEnum.ERROR2001);
        }
        return responseData;
    }

    @Override
    public ResponseData updateByCode(MaterialQuality materialQuality) {
        ResponseData responseData = new ResponseData();
        mapper.updateByCode(materialQuality);
        return responseData;
    }

    @Override
    public ResponseData deleteByCode(MaterialQuality materialQuality) {
        ResponseData responseData = new ResponseData();
        mapper.deleteByCode(materialQuality);
        return responseData;
    }

    @Override
    public ResponseData delete(MaterialQuality materialQuality) {
        ResponseData responseData = new ResponseData();
        Integer number =mapper.count(materialQuality);
        Integer semiProductCount =mapper.semiProductCount(materialQuality);

        if (materialQuality.getMakingsState().equals("原材料")){
            if (number==0){
                mapper.delete(materialQuality);
            }else {
                responseData.setCodeEnum(RespEnum.ERROR1016);
            }
        }else {
            if (materialQuality.getMakingsState().equals("半成品")){
                if (semiProductCount==0){
                    mapper.delete(materialQuality);
                }else {
                    responseData.setCodeEnum(RespEnum.ERROR1018);
                }
            }
        }

        return responseData;
    }

}
